package server_commands;

import static server.RLM_Server.activeConnections;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;

import server.ServerSocketConnection;
import connection_modul.DatabaseConnection;

public class UNREGCommand extends Command {
	
	
	public UNREGCommand(String parameter) {
		setCode(UNREG_CMD);
		setParameter(parameter);
	}
	
	/**
	 * Vrsi se deregistracija korisnika iz baze. Nakon predvidjenog izvrsenja ove
	 * metode, podaci o korisniku ce prestati da postoje u bazi.
	 * 
	 * 
	 */
	@SuppressWarnings("unchecked")
	public boolean executeCommand(ServerSocketConnection connection, DatabaseConnection dbConnection) {
		
		StringTokenizer tokenizer = new StringTokenizer(getParameter());
		String username = tokenizer.nextToken();
		String password = tokenizer.nextToken();
		
		try {
			dbConnection.doStatement("SELECT * FROM tblUsers WHERE username = " +
									"'" + username + "'");

			String pass = dbConnection.data.get(0).get(1).toString();

			if ( !password.equals(pass) ) {
				connection.setMessage("230 Wrong password");
				dbConnection.doCommit();
				return true;
			}
		
			dbConnection.clearDataStore();
			
			String SQLSelectQuery = "SELECT tblContacts.user_dest " +
			"FROM tblContacts " +
			"WHERE  tblContacts.user_source = '" + username + "' AND tblContacts.user_dest NOT IN " +
			"(SELECT tblBlocks.user_dest FROM tblBlocks WHERE tblBlocks.user_source = '" + username + "');";

			dbConnection.doStatement(SQLSelectQuery);
			
	
			// dobavljeni su username-ovi korisnika koje treba obavestiti o statusu
			// korisnika		
		
			ArrayList array = new ArrayList(dbConnection.data);
			
			dbConnection.clearDataStore();
			
			String blockNotInContactsQuery = "SELECT tblBlocks.user_source " +
						"FROM tblBlocks WHERE tblBlocks.user_dest = '" + username + "' " +
						"AND tblBlocks.user_source NOT IN (SELECT tblContacts.user_source FROM tblContacts WHERE tblContacts.user_dest = '" + username + "')";

			dbConnection.doStatement(blockNotInContactsQuery);
			
			array.addAll(dbConnection.data);
			
			dbConnection.clearDataStore();
		
			String updateSQLQuery = "DELETE FROM tblUsers " +
				"WHERE tblUsers.username = '" + username + "'";
		
			dbConnection.doUpdateStatement(updateSQLQuery);

			
			String deleteSQLUpdate = "DELETE FROM tblBlocks " +
			"WHERE tblBlocks.user_dest = '" + username + "' " +
			"AND tblBlocks.user_source NOT IN (SELECT tblContacts.user_source FROM tblContacts WHERE tblContacts.user_dest = '" + username + "')";
		
			
			dbConnection.doUpdateStatement(deleteSQLUpdate);
			
			if(array.size() == 0) {
//				connection.setMessage("100 Unregistration OK");
				dbConnection.doCommit();
				return true;
			}
		
			String message = "SERV_UNREG " + username;
		
			// Obavestava sve korisnike o deregistraciji datog korisnika

			Iterator usersIterator = array.iterator();
		
			while ( usersIterator.hasNext() ) {
			
				// 	username korisnika 
				String user = ((Vector)usersIterator.next()).get(0).toString();
			
				ServerSocketConnection tempConnection = activeConnections.get(user);
			
				if (tempConnection != null) {
					tempConnection.setMessage(message);
//					System.out.println("Korisnik koji se obavestava o delogovanju novog usera: " + user);
				}
			}
		
//			connection.setMessage("100 Unregistration OK");
		}
		catch(SQLException sqle) {
			dbConnection.doRollBack();
			sqle.printStackTrace();
			return false;
		}
		
		dbConnection.doCommit();
		
		return true;
	}

}
